// pages/service-role/index.js
const {registAndBind} = require('../../apiConfig/apiList/luban')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curValue: 0,
  },
  getUserRole(e){
    const curValue = e.currentTarget.dataset.id;
    wx.setStorageSync('userRole', curValue);
    this.setData({
      curValue: curValue
    });
  },
  goNext(){
    if(!this.data.curValue){
      wx.showToast({
        title: '请选择用户角色',
        icon: 'error'
      });
      return;
    }
    this.bandRLole();
  },
  bandRLole(){
    let encryptedData =  wx.getStorageSync('profile')?.encryptedData || {};
    let iv =  wx.getStorageSync('profile')?.iv || '';
    let userInfo =  wx.getStorageSync('userInfo') || {};
    let phoneEncryptedData =  wx.getStorageSync('phoneInfo')?.encryptedData || {};
    let phoneIv =  wx.getStorageSync('phoneInfo')?.iv || '';
    let loginData = wx.getStorageSync('loginData') || {};
    let wxCode = '';
    wx.login({
      success: (res) => {
        console.log('wxCode', res)
        wxCode =res.code;
        console.log('loginData', loginData)
        let params = {
          code: wxCode,
          maOpenId:  loginData.maOpenId,
          maSessionKey:  loginData.maSessionKey,
          maUnionId:  loginData.maUnionId,
          loginType: this.data.curValue,
          encryptedData: encryptedData,
          userInfo: userInfo,
          iv: iv,
          phoneEncryptedData: phoneEncryptedData,
          phoneIv: phoneIv,
        }
        registAndBind(params).then(res=>{
          console.log('registAndBind', res)
          let data = res.data;
          if(res.code == 200){
            let temp = wx.getStorageSync('loginData');
            console.log('data', data)
            temp.authToken = data.authToken;
            temp.loginUser = data.loginUser;
            wx.setStorageSync('loginData', temp);
            if(data.toBind == 1){
              if(data.loginType == 1){
                wx.navigateTo({
                  url: '/pages/login/login',
                })
              }else if(data.loginType == 2){
                wx.navigateTo({
                  url: '/pages/manage-address/index',
                })
              }
            }else if(data.toBind == 0){
              if(data.loginType == 1){
                wx.navigateTo({
                  url: '/pages/serviceHome/index',
                })
              }else if(data.loginType == 2){
                wx.navigateTo({
                  url: '/pages/home/index',
                })
              }
            }else if(data.toBind == -1){
              wx.redirectTo({
                url: '/pages/authLogin/index',
              })
            }
          }else{
            wx.redirectTo({
              url: '/pages/authLogin/index',
            })
          }
        }).catch(err=>{
          console.log('异常', err)
        })
      }
    });
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})